package com.example.oracle.controller;

import com.example.oracle.pojo.Task;
import com.example.oracle.pojo.system.RequestMap;
import com.example.oracle.pojo.system.ResponseMap;
import com.example.oracle.pojo.system.WebCode;
import com.example.oracle.service.TaskService;
import com.example.oracle.tool.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @author Siaze
 * @date 2021/11/23
 */
@RestController
@RequestMapping("/user")
public class TaskController {

    @Autowired
    TaskService taskService;

    @Autowired
    MailUtil mailUtil;

    private static final String ACCEPT_MESSAGE = "你的订单已被接单，详细信息请上线查看。";


    /**
     * 任务表查询
     *
     * @param task 限制条件
     * @return 数据列表
     */
    @PostMapping("/getTask")
    ResponseMap getTask(@RequestBody RequestMap<Task> task) {
        ResponseMap map = ResponseMap.create(WebCode.REQUEST_SUCCESS);
        map.putValue("list", taskService.selects(task));
        return map;
    }


    /**
     * 接单并邮箱通知
     * @param taskId 订单id
     * @return 操作是否成功
     */
    @GetMapping("/acceptTask")
    ResponseMap setTask(@RequestParam long taskId) {
        Task task = new Task();
        task.setState(2);
        task.setAccept(Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName()));
        task.setTaskId(taskId);
        if (taskService.updateTask(task) == 0) {
            return ResponseMap.create(WebCode.REQUEST_FAIL);
        }
        mailUtil.sendMessage(task.getId(),ACCEPT_MESSAGE);
        return ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }

    /**
     * 完成订单
     *
     * @param taskId 订单id
     * @return 操作是否成功
     */
    @GetMapping("/finishTask")
    ResponseMap finishTask(@RequestParam long taskId) {
        Task task = new Task();
        task.setState(3);
        task.setId(Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName()));
        task.setTaskId(taskId);
        return taskService.updateTask(task) == 0 ? ResponseMap.create(WebCode.REQUEST_FAIL) : ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }

    /**
     * 添加订单
     *
     * @param task 订单信息
     * @return 操作是否成功
     */
    @PostMapping("/addTask")
    ResponseMap addTask(@RequestBody Task task) {
        task.setId(Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName()));
        task.setTime(new Date());
        return taskService.insertTask(task) == 0 ? ResponseMap.create(WebCode.REQUEST_FAIL) : ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }

    /**
     * 取消订单
     * @param taskId 需要取消的TaskId
     */
    @PostMapping("/cancelTask")
    ResponseMap cancelTask(@RequestBody Long taskId){
        Task task = new Task();
        task.setTaskId(taskId);
        task.setState(1);
        taskService.updateTask(task);
        return ResponseMap.create(WebCode.REQUEST_SUCCESS);
    }


}
